package cn.mumzone.basic.util;

import java.math.BigDecimal;

/**
 * Created by qiuzhenhuan on 2019/3/4.
 */

public class BigDecimalUtil {


    private static final int DEF_DIV_SCALE = 10;


    public static double add(String number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.add(numberB2).doubleValue();
    }

    /**
     * 加
     * @param number1
     * @param number2
     * @return
     */
    public static double add(double number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.setScale(2, BigDecimal.ROUND_HALF_UP).add(numberB2).doubleValue();
    }

    /**
     * 减
     * @param number1
     * @param number2
     * @return
     */
    public static double sub(String number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.subtract(numberB2).doubleValue();
    }

    /**
     * 乘
     * @param number1
     * @param number2
     * @return
     */
    public static double mul(String number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.multiply(numberB2).doubleValue();
    }

    /**
     * 除
     * @param number1
     * @param number2
     * @return
     */
    public static double div(String number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.divide(numberB2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static float divRetrunFloat(String number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.divide(numberB2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    /**
     * 比较两个数字大小
     * @param number1
     * @param number2
     * @return 大于 1 小于 -1 等于 0
     */
    public static int compareTo(String number1, String number2) {
        BigDecimal numberB1 = new BigDecimal(number1);
        BigDecimal numberB2 = new BigDecimal(number2);
        return numberB1.compareTo(numberB2);
    }


    public static String getNumber(String number) {
        return null != number && !"".equals(number) ? new BigDecimal(number).toPlainString() : "0";
    }

    public static String getStringNumberFromFloat(float number) {
        BigDecimal number1 = new BigDecimal(number);

        return number1.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
    }
}
